//
//  GetcashViewController.m
//  Ten
//
//  Created by anfuer on 2018/8/20.
//  Copyright © 2018年 xg韦. All rights reserved.
//

#import "GetcashViewController.h"
#import "UIButton+countDown.h"
#import "WxbkViewController.h"


@interface GetcashViewController ()
{
    double mymonry;
}
@property (weak, nonatomic) IBOutlet UILabel *titleLB;
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet UILabel *lb1;
@property (weak, nonatomic) IBOutlet UILabel *lb2;
@property (weak, nonatomic) IBOutlet UITextField *tsf1;
@property (weak, nonatomic) IBOutlet UILabel *lb3;
@property (weak, nonatomic) IBOutlet UITextField *tsf2;
@property (weak, nonatomic) IBOutlet UIButton *codeBtno;
@property (weak, nonatomic) IBOutlet UILabel *tipLb;

@end

@implementation GetcashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"1.1000元以下提现,按次数收取手续费，每次一元,将会从系统内自动扣除。\n2.提现1000元以上免扣除手续费。\n3.如选择全部提现且提现金额小于1000元系统会自动在全部提现额内减1元预留作扣除手续费的用途。"];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:4];
    [str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [str length])];
    _tipLb.attributedText=str;
    
    if ([_type isEqualToString:@"zfb"]) {
        _titleLB.text=@"提现到支付宝";
        _img.image=WXGImage(@"支付宝");
        _lb1.text=WXGStr(_passdic[@"alipay_real_name"]);
        _lb2.text=@"更换支付宝账号";
    }else if ([_type isEqualToString:@"Bank"])
    {
        
        _titleLB.text=[NSString stringWithFormat:@"提现到%@",_passdic[@"bank_name"]];
        _img.image=WXGImage(_passdic[@"bank_name"]);
        _lb1.text=WXGStr(_passdic[@"account"]);
        mymonry=[_passdic[@"expense"] doubleValue];
        _lb2.text=@"更换银行卡";
        
    }
    else
    {
         _lb1.text=WXGStr(_passdic[@"wx_nickname"]);
    }
    _lb3.text=[NSString stringWithFormat:@"可提现余额:%@",_passdic[@"expense"]];
    // Do any additional setup after loading the view.
}
- (IBAction)getCode:(UIButton *)sender {
    [FuncPublic getData:self parameters:nil RequestUrl:Cbsend_code success:^(NSDictionary *acountInfo) {
        
        [WSProgressHUD showImage:nil status:@"发送成功"];
        [_tsf2 becomeFirstResponder];
        _codeBtno.countDownFormat=@"重新发送%ds";
        [_codeBtno countDownWithTimeInterval:49];
    }];
    
}
- (IBAction)AllinBtna:(UIButton *)sender {
    _tsf1.text=WXGStr(_passdic[@"expense"]);
    if ((mymonry-[_tsf1.text doubleValue]<1)&&[_tsf1.text doubleValue]<1000)
    {
        [WSProgressHUD showImage:nil status:@"您余额不够抵扣手续费，系统将自动在提现金额帮您扣1"];
        _tsf1.text=[NSString stringWithFormat:@"%.2f",[_tsf1.text doubleValue]-1];
    }
}
- (IBAction)btna:(UIButton *)sender {
    NSDictionary*dic;
    if ([_type isEqualToString:@"Bank"]) {
        dic=@{@"type":@"3",@"salary":_tsf1.text,@"code":_tsf2.text,@"bank_card_id":WXGStr(_passdic[@"id"])};
    }else
    {
        dic=@{@"type":[_type isEqualToString:@"zfb"]? @"1":[_type isEqualToString:@"wx"]?@"2":@"3",@"salary":_tsf1.text,@"code":_tsf2.text};
    }
    
    [FuncPublic getData:self parameters:dic RequestUrl:Ccash_draw_add success:^(NSDictionary *acountInfo) {
       [WSProgressHUD showImage:nil status:@"提现申请提交成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }];

}

- (IBAction)ChanggeBtna:(UIButton *)sender {
    if ([_type isEqualToString:@"Bank"])
    {
        
        
    }else
    {
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"bk" bundle:nil];
        WxbkViewController*secondViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"WxbkView"];
        secondViewController.type=_type;
        secondViewController.passdic=_passdic;
        [self.navigationController pushViewController:secondViewController animated:YES];

    }
 
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//判断是否大于1000
- (void)textFieldDidEndEditing:(UITextField *)textField;{
    if ([textField.text doubleValue]<1000) {
         [WSProgressHUD showImage:nil status:@"您的提现金额少于1000元将扣除1元手续费"];
    }
    NSLog(@"%.2f----%.2f",mymonry,[textField.text doubleValue]);
    if ((mymonry-[textField.text doubleValue]<1)&&[textField.text doubleValue]<1000)
    {
        [WSProgressHUD showImage:nil status:@"您余额不够抵扣手续费，系统将自动在提现金额帮您扣1"];
        textField.text=[NSString stringWithFormat:@"%.2f",[textField.text doubleValue]-1];
    }
    
}


#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // _textFiledM1  项目中的tf控件
    if (textField == _tsf1) {
        
        // 1 不能直接输入小数点
        if ( [textField.text isEqualToString:@""] && [string isEqualToString:@"."] )  return NO;
        
        
        // 2 输入框第一个字符为“0”时候，第二个字符如果不是“.”,那么文本框内的显示内容就是新输入的字符[textField.text length] == 1  防止例如0.5会变成5
        NSRange zeroRange = [textField.text rangeOfString:@"0"];
        if(zeroRange.length == 1 && [textField.text length] == 1 && ![string isEqualToString:@"."]){
            textField.text = string;
            return NO;
        }
        
        
        // 3 保留两位小数
        NSUInteger remain = 2;
        NSRange pointRange = [textField.text rangeOfString:@"."];
        
        // 拼接输入的最后一个字符
        NSString *tempStr = [textField.text stringByAppendingString:string];
        NSUInteger strlen = [tempStr length];
        
        // 输入框内存在小数点， 不让再次输入“.” 或者 总长度-包括小数点之前的长度>remain 就不能再输入任何字符
        if(pointRange.length > 0 &&([string isEqualToString:@"."] || strlen - (pointRange.location + 1) > remain))
            return NO;
        
        
        // 4 小数点已经存在情况下可以输入的字符集  and 小数点还不存在情况下可以输入的字符集
        NSCharacterSet *numbers = (pointRange.length > 0)?[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] : [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
        
        NSScanner *scanner = [NSScanner scannerWithString:string];
        NSString *buffer;
        // 判断string在不在numbers的字符集合内
        BOOL scan = [scanner scanCharactersFromSet:numbers intoString:&buffer];
        
        if ( !scan && ([string length] != 0) )  // 包括输入空格scan为NO， 点击删除键[string length]为0
        {
            return NO;
        }
        
    }
    
    return YES;
}
- (IBAction)telBtna:(UIButton *)sender {
    
         NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"4006663186"];
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}


@end
